package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.adapter.CommonAbsViewHolder;
import com.chexiehui.jc.adapter.CommonStickyListHeaderAdapter;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.entity.CarInfo;
import com.chexiehui.jc.entity.CarPkListInfo;
import com.chexiehui.jc.model.PkLeftModel;
import com.chexiehui.jc.model.PkRightChildModel;
import com.chexiehui.jc.model.PkRightModel;
import com.chexiehui.jc.mvp.car.CarBrandParameterCompareCovenant;
import com.chexiehui.jc.mvp.car.CarBrandParameterComparePresenter;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.chexiehui.jc.widget.NonSlidingGridView;
import com.chexiehui.jc.widget.NonStickyListHeadersListView;
import com.chexiehui.jc.widget.SyncHorizontalScrollView;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: SongJun.
 * CreateDate: 2019/1/21 0021.
 * Description: 参数对比
 */

public class CarBrandParameterCompare2Fragment extends BaseMvpFragment<CarBrandParameterComparePresenter> implements CarBrandParameterCompareCovenant.View {

    /*@BindView(R.id.tv_table_title_left)
    TextView tvTableTitleLeft;
    @BindView(R.id.right_title_container)
    LinearLayout rightTitleContainer;
    @BindView(R.id.title_horsv)
    SyncHorizontalScrollView titleHorsv;
    @BindView(R.id.left_container_listview)
    NoScrollListView leftContainerListview;
    @BindView(R.id.right_container_listview)
    NoScrollListView rightContainerListview;
    @BindView(R.id.content_horsv)
    SyncHorizontalScrollView contentHorsv;
    @BindView(R.id.pull_refresh_scroll)
    NestedScrollView pullRefreshScroll;


    //表格部分
    private AbsCommonAdapter<TableModel> mLeftAdapter, mRightAdapter;*/

    //    @BindView(R.id.sb_switch_toggle)
//    SwitchButton sbSwitchToggle;
    @BindView(R.id.rl_switch_container)
    RelativeLayout rlSwitchContainer;
    @BindView(R.id.nsgv_title_grid)
    NonSlidingGridView nsgvTitleGrid;
    @BindView(R.id.shsv_title_scroller)
    SyncHorizontalScrollView shsvTitleScroller;
    @BindView(R.id.nslv_left_list)
    NonStickyListHeadersListView nslvLeftList;
    @BindView(R.id.nslv_right_list)
    NonStickyListHeadersListView nslvRightList;
    @BindView(R.id.shsv_right_content)
    SyncHorizontalScrollView shsvRightContent;
    @BindView(R.id.nsv_bottom_scroller)
    NestedScrollView nsvBottomScroller;

    private List<CarInfo> compareList;
    private CommonStickyListHeaderAdapter<PkLeftModel> leftListAdapter;
    private List<PkLeftModel> leftListData;
    private CommonStickyListHeaderAdapter<PkRightModel> rightListAdapter;
    private List<PkRightModel> rightListData;
    private List<CarPkListInfo> titleGridData;
    private CommonAdapter<CarPkListInfo> titleGridAdapter;

    public static CarBrandParameterCompare2Fragment getInstance(ArrayList<CarInfo> compareList) {
        CarBrandParameterCompare2Fragment fragment = new CarBrandParameterCompare2Fragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("compareList", compareList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_car_brand_parameter_compare;
    }

    @Override
    protected void beforeSetView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            compareList = (ArrayList<CarInfo>) bundle.getSerializable("compareList");
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        if (compareList == null && compareList.size() < 1) {
            return;
        }
        titleGridData = new ArrayList<>();
        nsgvTitleGrid.setAdapter(titleGridAdapter = new CommonAdapter<CarPkListInfo>(mActivity, R.layout.item_car_pk_title_grid_car, titleGridData) {
            @Override
            protected void convert(ViewHolder viewHolder, CarPkListInfo item, int position) {
                viewHolder.setText(R.id.tv_item_name, item.getSpecName());
                viewHolder.getView(R.id.iv_item_delete).setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        removePkItem(position);
                    }
                });
            }
        });

        leftListData = new ArrayList<>();
        /*leftListAdapter = new CommonAdapter<PkLeftModel>(mActivity, R.layout.item_car_pk_left_params, leftListData) {
            @Override
            protected void convert(ViewHolder viewHolder, PkLeftModel item, int position) {
                viewHolder.setText(R.id.tv_item_name, item.getParamName());
            }
        };*/
        leftListAdapter = new CommonStickyListHeaderAdapter<PkLeftModel>(mActivity, R.layout.item_car_pk_left_header, R.layout.item_car_pk_left_params, leftListData) {

            @Override
            protected void convertItem(CommonAbsViewHolder viewHolder, PkLeftModel item, int position) {
                viewHolder.setText(R.id.tv_item_name, item.getParamName());
            }

            @Override
            protected void convertHead(CommonAbsViewHolder viewHolder, PkLeftModel item, int position) {
                viewHolder.setText(R.id.tv_item_titme, item.getGroupName());
            }
        };
        nslvLeftList.setAdapter(leftListAdapter);

        rightListData = new ArrayList<>();
        rightListAdapter = new CommonStickyListHeaderAdapter<PkRightModel>(mActivity, R.layout.item_car_pk_left_header, R.layout.item_car_pk_right_container, rightListData) {
            @Override
            protected void convertItem(CommonAbsViewHolder viewHolder, PkRightModel item, int position) {
                NonSlidingGridView nsgv_item_params = viewHolder.getView(R.id.nsgv_item_params);
                nsgv_item_params.setNumColumns(item.getValueList().size());
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) nsgv_item_params.getLayoutParams();
                lp.width = item.getValueList().size() * SizeUtils.dp2px(123);
                nsgv_item_params.setLayoutParams(lp);
                nsgv_item_params.setAdapter(new CommonAdapter<PkRightChildModel>(mActivity, R.layout.item_car_pk_right_params, item.getValueList()) {
                    @Override
                    protected void convert(ViewHolder childHolder, PkRightChildModel childItem, int childPosition) {
                        childHolder.setText(R.id.tv_item_value, childItem.getParamValue());
                    }
                });
            }

            @Override
            protected void convertHead(CommonAbsViewHolder viewHolder, PkRightModel item, int position) {
                viewHolder.setText(R.id.tv_item_titme, item.getGroupName());
            }
        };
        nslvRightList.setAdapter(rightListAdapter);
        shsvTitleScroller.setScrollView(shsvRightContent);
        shsvRightContent.setScrollView(shsvTitleScroller);
        mvpPresenter.getPkInfo();
    }

    private void removePkItem(int position) {
        compareList.remove(position);
        titleGridData.remove(position);
        leftListData.clear();
        rightListData.clear();
        setTableData();
    }

    private void setTableData() {
        if (titleGridData.size() > 0) {
            for (int i = 0; i < titleGridData.get(0).getGroupParamsViewModelList().size(); i++) {
                for (int j = 0; j < titleGridData.get(0).getGroupParamsViewModelList().get(i).getParamList().size(); j++) {
                    leftListData.add(new PkLeftModel(titleGridData.get(0).getGroupParamsViewModelList().get(i).getGroupName(), i,
                            titleGridData.get(0).getGroupParamsViewModelList().get(i).getParamList().get(j).getParamId(),
                            titleGridData.get(0).getGroupParamsViewModelList().get(i).getParamList().get(j).getParamName()));
                }
            }
            for (int i = 0; i < leftListData.size(); i++) {
                List<PkRightChildModel> list = new ArrayList<>();
                for (int j = 0; j < compareList.size(); j++) {
                    list.add(new PkRightChildModel(
                            titleGridData.get(j).getSpecId(),
                            titleGridData.get(j).getSpecName(),
                            titleGridData.get(j).getAllParamValue().get(i).getParamValue()));
                }
                rightListData.add(new PkRightModel(leftListData.get(i).getGroupName(), leftListData.get(i).getGroupIndex(),
                        leftListData.get(i).getParamName(),
                        leftListData.get(i).getParamId(), list));
            }
        }
        nsgvTitleGrid.setNumColumns(titleGridData.size());
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) nsgvTitleGrid.getLayoutParams();
        lp.width = compareList.size() * SizeUtils.dp2px(123);
        nsgvTitleGrid.setLayoutParams(lp);
        lp =  (LinearLayout.LayoutParams) nslvRightList.getLayoutParams();
        lp.width = compareList.size() * SizeUtils.dp2px(123);
        nslvRightList.setLayoutParams(lp);

        titleGridAdapter.notifyDataSetChanged();
        leftListAdapter.notifyDataSetChanged();
        rightListAdapter.notifyDataSetChanged();
    }

    /*

    @Override
    protected void initView(Bundle savedInstanceState) {
        getLayoutInflater().inflate(R.layout.table_right_title, rightTitleContainer);
        titleHorsv.setScrollView(contentHorsv);
        contentHorsv.setScrollView(titleHorsv);
        initTableView();
        setListener();
        setData();
        mvpPresenter.getPkInfo();
    }

    public void initTableView() {
        mLeftAdapter = new AbsCommonAdapter<TableModel>(mActivity, R.layout.table_left_item) {
            @Override
            public void convert(AbsViewHolder helper, TableModel item, int pos) {
                TextView tv_table_content_left = helper.getView(R.id.tv_table_content_item_left);
                tv_table_content_left.setText(item.getLeftTitle());
            }
        };
        mRightAdapter = new AbsCommonAdapter<TableModel>(mActivity, R.layout.table_right_item) {
            @Override
            public void convert(AbsViewHolder helper, TableModel item, int pos) {
                TextView tv_table_content_right_item0 = helper.getView(R.id.tv_table_content_right_item0);
                TextView tv_table_content_right_item1 = helper.getView(R.id.tv_table_content_right_item1);
                TextView tv_table_content_right_item2 = helper.getView(R.id.tv_table_content_right_item2);
                TextView tv_table_content_right_item3 = helper.getView(R.id.tv_table_content_right_item3);
                TextView tv_table_content_right_item4 = helper.getView(R.id.tv_table_content_right_item4);
                TextView tv_table_content_right_item5 = helper.getView(R.id.tv_table_content_right_item5);
                TextView tv_table_content_right_item6 = helper.getView(R.id.tv_table_content_right_item6);
                TextView tv_table_content_right_item7 = helper.getView(R.id.tv_table_content_right_item7);
                TextView tv_table_content_right_item8 = helper.getView(R.id.tv_table_content_right_item8);
                TextView tv_table_content_right_item9 = helper.getView(R.id.tv_table_content_right_item9);
                TextView tv_table_content_right_item10 = helper.getView(R.id.tv_table_content_right_item10);
                TextView tv_table_content_right_item11 = helper.getView(R.id.tv_table_content_right_item11);
                TextView tv_table_content_right_item12 = helper.getView(R.id.tv_table_content_right_item12);
                TextView tv_table_content_right_item13 = helper.getView(R.id.tv_table_content_right_item13);
                TextView tv_table_content_right_item14 = helper.getView(R.id.tv_table_content_right_item14);

                tv_table_content_right_item0.setText(item.getText0());
                tv_table_content_right_item1.setText(item.getText1());
                tv_table_content_right_item2.setText(item.getText2());
                tv_table_content_right_item3.setText(item.getText3());
                tv_table_content_right_item4.setText(item.getText4());
                tv_table_content_right_item5.setText(item.getText5());
                tv_table_content_right_item6.setText(item.getText6());
                tv_table_content_right_item7.setText(item.getText7());
                tv_table_content_right_item8.setText(item.getText8());
                tv_table_content_right_item9.setText(item.getText9());
                tv_table_content_right_item10.setText(item.getText10());
                tv_table_content_right_item11.setText(item.getText11());
                tv_table_content_right_item12.setText(item.getText12());
                tv_table_content_right_item13.setText(item.getText13());
                tv_table_content_right_item14.setText(item.getText14());

                //部分行设置颜色凸显
                item.setTextColor(tv_table_content_right_item0, item.getText0());
                item.setTextColor(tv_table_content_right_item5, item.getText5());
                item.setTextColor(tv_table_content_right_item10, item.getText10());
                item.setTextColor(tv_table_content_right_item14, item.getText14());

                for (int i=0; i<15; i++) {
                    View view = ((LinearLayout) helper.getConvertView()).getChildAt(i);
                    view.setVisibility(View.VISIBLE);
                }
            }
        };
        leftContainerListview.setAdapter(mLeftAdapter);
        rightContainerListview.setAdapter(mRightAdapter);
    }


    public void setListener() {
        leftContainerListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //跳转界面
               showToast("打开某条记录的单独详情");
            }
        });
    }

    public void setData(){
        List<OnlineSaleBean> onlineSaleBeanList = new ArrayList<>();
        for(int i=0;i < 80;i++){
            onlineSaleBeanList.add(new OnlineSaleBean("行标题"+i));
        }

        List<TableModel> mDatas = new ArrayList<>();
        for (int i = 0; i < onlineSaleBeanList.size(); i++) {
            OnlineSaleBean onlineSaleBean = onlineSaleBeanList.get(i);
            TableModel tableMode = new TableModel();
            tableMode.setOrgCode(onlineSaleBean.getOrgCode());
            tableMode.setLeftTitle(onlineSaleBean.getCompanyName());
            tableMode.setText0(onlineSaleBean.getOrgCode()+"");//列0内容
            tableMode.setText1(onlineSaleBean.getAreaName()+"");//列1内容
            tableMode.setText2(onlineSaleBean.getSaleAll() + "");//列2内容
            tableMode.setText3(onlineSaleBean.getSaleAllOneNow() + "");
            tableMode.setText4(onlineSaleBean.getSaleAllLast() + "");
            tableMode.setText5(onlineSaleBean.getSaleAllOneNowLast() + "");//
            tableMode.setText6(onlineSaleBean.getSaleAllRate() + "");//
            tableMode.setText7(onlineSaleBean.getSaleAllOneNowRate() + "");//
            tableMode.setText8(onlineSaleBean.getRetailSale() + "");//
            tableMode.setText9(onlineSaleBean.getRetailSaleOneNow() + "");//
            tableMode.setText10(onlineSaleBean.getRetailSaleLast() + "");//
            tableMode.setText11(onlineSaleBean.getRetailSaleOneNowLast() + "");//
            tableMode.setText12(onlineSaleBean.getRetailSaleRate() + "");//
            tableMode.setText13(onlineSaleBean.getRetailSaleOneNowRate() + "");//
            tableMode.setText14(onlineSaleBean.getOnlineSale() + "");//
            mDatas.add(tableMode);
        }
        mLeftAdapter.addData(mDatas, true);
        mRightAdapter.addData(mDatas, true);
        mDatas.clear();
    }*/

    @Override
    protected CarBrandParameterComparePresenter createPresenter() {
        return new CarBrandParameterComparePresenter(this);
    }

    @Override
    public void onGetPkInfoSuccess(BaseModel<List<CarPkListInfo>> successResult) {
        titleGridData.clear();
        titleGridData.addAll(successResult.getData());
        leftListData.clear();
        rightListData.clear();
        setTableData();
    }

    @Override
    public void onGetPkInfoFailure(BaseModel<Object> failureResult) {

    }

    @Override
    public String getCarIds() {
        if (compareList == null || compareList.size() < 1) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < compareList.size(); i++) {
            sb.append(compareList.get(i).getId());
            if (i != compareList.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    @OnClick(R.id.rl_switch_container)
    public void onViewClicked() {
    }

/*
    public class OnlineSaleBean{

        private int id;
        private String addTime;
        private String modTime;
        private String nianYue;
        private String areaCode;
        private String orgCode;
        private String status;
        private String saleAll;
        private String saleAllOneNow;
        private String retailSale;
        private String retailSaleOneNow;
        private String onlineSale;
        private String onlineSaleOneNow;
        private String retailOnlineSale;
        private String retailOnlineSaleOneNow;
        private String addUserId;
        private String addUser;
        private String areaName;
        private String companyName;
        private String saleAllLast;
        private String saleAllOneNowLast;
        private String retailSaleLast;
        private String retailSaleOneNowLast;
        private String onlineSaleLast;
        private String onlineSaleOneNowLast;
        private String retailOnlineSaleLast;
        private String retailOnlineSaleOneNowLast;
        private String saleAllRate;
        private String saleAllOneNowRate;
        private String retailSaleRate;
        private String retailSaleOneNowRate;
        private String onlineSaleRate;
        private String onlineSaleOneNowRate;
        private String retailOnlineSaleRate;
        private String retailOnlineSaleOneNowRate;

        public OnlineSaleBean(String companyName){
            this.companyName = companyName;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getAddTime() {
            return addTime;
        }

        public void setAddTime(String addTime) {
            this.addTime = addTime;
        }

        public String getModTime() {
            return modTime;
        }

        public void setModTime(String modTime) {
            this.modTime = modTime;
        }

        public String getNianYue() {
            return nianYue;
        }

        public void setNianYue(String nianYue) {
            this.nianYue = nianYue;
        }

        public String getAreaCode() {
            return areaCode;
        }

        public void setAreaCode(String areaCode) {
            this.areaCode = areaCode;
        }

        public String getOrgCode() {
            return orgCode;
        }

        public void setOrgCode(String orgCode) {
            this.orgCode = orgCode;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getSaleAll() {
            return saleAll;
        }

        public void setSaleAll(String saleAll) {
            this.saleAll = saleAll;
        }

        public String getSaleAllOneNow() {
            return saleAllOneNow;
        }

        public void setSaleAllOneNow(String saleAllOneNow) {
            this.saleAllOneNow = saleAllOneNow;
        }

        public String getRetailSale() {
            return retailSale;
        }

        public void setRetailSale(String retailSale) {
            this.retailSale = retailSale;
        }

        public String getRetailSaleOneNow() {
            return retailSaleOneNow;
        }

        public void setRetailSaleOneNow(String retailSaleOneNow) {
            this.retailSaleOneNow = retailSaleOneNow;
        }

        public String getOnlineSale() {
            return onlineSale;
        }

        public void setOnlineSale(String onlineSale) {
            this.onlineSale = onlineSale;
        }

        public String getOnlineSaleOneNow() {
            return onlineSaleOneNow;
        }

        public void setOnlineSaleOneNow(String onlineSaleOneNow) {
            this.onlineSaleOneNow = onlineSaleOneNow;
        }

        public String getRetailOnlineSale() {
            return retailOnlineSale;
        }

        public void setRetailOnlineSale(String retailOnlineSale) {
            this.retailOnlineSale = retailOnlineSale;
        }

        public String getRetailOnlineSaleOneNow() {
            return retailOnlineSaleOneNow;
        }

        public void setRetailOnlineSaleOneNow(String retailOnlineSaleOneNow) {
            this.retailOnlineSaleOneNow = retailOnlineSaleOneNow;
        }

        public String getAddUserId() {
            return addUserId;
        }

        public void setAddUserId(String addUserId) {
            this.addUserId = addUserId;
        }

        public String getAddUser() {
            return addUser;
        }

        public void setAddUser(String addUser) {
            this.addUser = addUser;
        }

        public String getAreaName() {
            return areaName;
        }

        public void setAreaName(String areaName) {
            this.areaName = areaName;
        }

        public String getCompanyName() {
            return companyName;
        }

        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }

        public String getSaleAllLast() {
            return saleAllLast;
        }

        public void setSaleAllLast(String saleAllLast) {
            this.saleAllLast = saleAllLast;
        }

        public String getSaleAllOneNowLast() {
            return saleAllOneNowLast;
        }

        public void setSaleAllOneNowLast(String saleAllOneNowLast) {
            this.saleAllOneNowLast = saleAllOneNowLast;
        }

        public String getRetailSaleLast() {
            return retailSaleLast;
        }

        public void setRetailSaleLast(String retailSaleLast) {
            this.retailSaleLast = retailSaleLast;
        }

        public String getRetailSaleOneNowLast() {
            return retailSaleOneNowLast;
        }

        public void setRetailSaleOneNowLast(String retailSaleOneNowLast) {
            this.retailSaleOneNowLast = retailSaleOneNowLast;
        }

        public String getOnlineSaleLast() {
            return onlineSaleLast;
        }

        public void setOnlineSaleLast(String onlineSaleLast) {
            this.onlineSaleLast = onlineSaleLast;
        }

        public String getOnlineSaleOneNowLast() {
            return onlineSaleOneNowLast;
        }

        public void setOnlineSaleOneNowLast(String onlineSaleOneNowLast) {
            this.onlineSaleOneNowLast = onlineSaleOneNowLast;
        }

        public String getRetailOnlineSaleLast() {
            return retailOnlineSaleLast;
        }

        public void setRetailOnlineSaleLast(String retailOnlineSaleLast) {
            this.retailOnlineSaleLast = retailOnlineSaleLast;
        }

        public String getRetailOnlineSaleOneNowLast() {
            return retailOnlineSaleOneNowLast;
        }

        public void setRetailOnlineSaleOneNowLast(String retailOnlineSaleOneNowLast) {
            this.retailOnlineSaleOneNowLast = retailOnlineSaleOneNowLast;
        }

        public String getSaleAllRate() {
            return saleAllRate;
        }

        public void setSaleAllRate(String saleAllRate) {
            this.saleAllRate = saleAllRate;
        }

        public String getSaleAllOneNowRate() {
            return saleAllOneNowRate;
        }

        public void setSaleAllOneNowRate(String saleAllOneNowRate) {
            this.saleAllOneNowRate = saleAllOneNowRate;
        }

        public String getRetailSaleRate() {
            return retailSaleRate;
        }

        public void setRetailSaleRate(String retailSaleRate) {
            this.retailSaleRate = retailSaleRate;
        }

        public String getRetailSaleOneNowRate() {
            return retailSaleOneNowRate;
        }

        public void setRetailSaleOneNowRate(String retailSaleOneNowRate) {
            this.retailSaleOneNowRate = retailSaleOneNowRate;
        }

        public String getOnlineSaleRate() {
            return onlineSaleRate;
        }

        public void setOnlineSaleRate(String onlineSaleRate) {
            this.onlineSaleRate = onlineSaleRate;
        }

        public String getOnlineSaleOneNowRate() {
            return onlineSaleOneNowRate;
        }

        public void setOnlineSaleOneNowRate(String onlineSaleOneNowRate) {
            this.onlineSaleOneNowRate = onlineSaleOneNowRate;
        }

        public String getRetailOnlineSaleRate() {
            return retailOnlineSaleRate;
        }

        public void setRetailOnlineSaleRate(String retailOnlineSaleRate) {
            this.retailOnlineSaleRate = retailOnlineSaleRate;
        }

        public String getRetailOnlineSaleOneNowRate() {
            return retailOnlineSaleOneNowRate;
        }

        public void setRetailOnlineSaleOneNowRate(String retailOnlineSaleOneNowRate) {
            this.retailOnlineSaleOneNowRate = retailOnlineSaleOneNowRate;
        }
    }*/
}
